<template>
  <div>
    <el-form ref="addleaveform" :rules="addRules" :model="wareData" label-width="120px">
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="wareData.name" placeholder="请输入仓库名称" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="realname"> 
        <el-input v-model="wareData.realname" placeholder="请输入联系人姓名" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model.number="wareData.phone" placeholder="请输入电话" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="仓库地址" prop="addr">
        <el-input v-model="wareData.addr" placeholder="请输入地址" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="经纬度" prop="coordinate">
        <el-input v-model="wareData.coordinate" placeholder="请输入经纬度" size="small" style="max-width:70%"></el-input>
        <el-button type="text" @click="disMap = true">点击获取坐标</el-button>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="wareData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>

    <!-- 地图 -->
    <el-dialog :visible.sync="disMap" title="地图" :modal="false">
      <!-- <zm-map 
        :center="wareData.coordinate === '' || wareData.coordinate === undefined || wareData.coordinate === null ? 
        (wareData.addr === '' ? {lng: 116.404, lat: 39.915} : wareData.addr) : {lng: wareData.coordinate.split(',')[0], lat: wareData.coordinate.split(',')[1]}" 
        @selectPoint="selectPoint"/> -->
      <zm-map 
        :center="wareData.addr === '' ? {lng: 116.404, lat: 39.915} : wareData.addr" 
        @selectPoint="selectPoint"/>
    </el-dialog>
  </div>
</template>

<script>
import store from '@/store'
import { postWare, putWare } from '@/api/customers'
import ZmMap from '@/components/ZmMap'

export default {
  name: 'addLeave',
  components: {
    ZmMap
  },
  props: {
    isAdd: {// 是否是新增
      type: Boolean,
      required: true,
      default: true
    },
    wareData: {
      type: Object,
      default: () => {
        return {
          key: store.state.app.activeApp.saa_key,
          name: '',
          realname: '',
          phone: '',
          addr: '',
          coordinate: '',
          status: '1',
        }
      }
    }
  },
  data() {
    return {
      disMap: false,
      addRules: {
        name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入联系人姓名', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { type: 'number', message: '电话必须为数字', trigger: 'blur' }
        ],
        addr: [
          { required: true, message: '请输入地址', trigger: 'blur' },
        ],
        coordinate: [
          { required: true, message: '请输入经纬度', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.addleaveform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            postWare(this.wareData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            putWare(this.wareData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    },
    /**
     * 选择经纬度
     */
    selectPoint(point) {
      this.wareData.coordinate = point.lng+','+point.lat;
      this.disMap = false;
    }
  },
}
</script>